<template>
<div>

<!--  <video-player  class="video-player vjs-custom-skin" style="margin-bottom: 20px;" ref="videoPlayer" :playsinline="true"
                 :options="playerOptions"
  ></video-player>-->
  <video-player class="video-player vjs-custom-skin"
                ref="videoPlayer"
                :playsinline="true"
                :options="playerOptions"
                @play="onPlayerPlay($event)"
                @pause="onPlayerPause($event)"
                @ended="onPlayerEnded($event)"
                @loadeddata="onPlayerLoadeddata($event)"
                @waiting="onPlayerWaiting($event)"
                @playing="onPlayerPlaying($event)"
                @timeupdate="onPlayerTimeupdate($event)"
                @canplay="onPlayerCanplay($event)"
                @canplaythrough="onPlayerCanplaythrough($event)"
                @ready="playerReadied"
                @statechanged="playerStateChanged($event)">
  </video-player>


</div>
</template>

<script>
import { videoPlayer } from 'vue-video-player'
import 'video.js/dist/video-js.css'
import qs from "qs";
export default {
name: "Pvideo",
  data () {
    return {
      paused: true,
      learningDuration: {
        id: '',
        userId: '',
        type: '0',
        examinationId: '',
        finishFlag: '0',
        durations: ''
      },
      playerOptions: {
        playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
        autoplay: false, // 如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: 'auto', // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: 'zh-CN',
        aspectRatio: '16:9', // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: false, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [{
          // src: '../../static/video/test.mp4', // 路径
          src: 'http://static.smartisanos.cn/common/video/t1-ui.mp4'
          // type: 'video/mp4' // 类型
        }],
        poster: '../../../static/images/home/cnooc.png', // 你的封面地址
        // width: document.documentElement.clientWidth,
        notSupportedMessage: '此视频暂无法播放，请稍后再试', // 允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true // 全屏按钮
        }
      }
    }
  },mounted () {
    this.timer = setInterval(this.putLearningObj, 3000)
  },
  destroyed () {
    // 如果定时器在运行则关闭
    if (this.timer) {
      clearInterval(this.timer)
    }
  },
  methods: {
  putLearningObj () {
      if (!this.paused) {
        this.putObj(this.learningDuration)
        console.log('putLearningObj ~~~~~~~~~')
      }
    },
  putObj(learningDuration){
    // console.log("这个地方请求后台radis");
    // console.log(learningDuration);
    //console.log("learningDuration======="+JSON.stringify(this.learningDuration));
    console.log("durations====="+this.durations);
   /* if(learningDuration.minutia.length !== 0){
      console.log(this.learningDuration)
      /!*this.$axios.post("course/lhx/learningduration/putObj",qs.stringify(learningDuration)).then(r=>{})*!/
    }*/
  },
    //监听播放
    onPlayerPlay (player) {
      console.log(player);
      this.paused = false
      // this.$refs.videoPlayer.player.play();
    },
    //监听暂停
    onPlayerPause (player) {
      this.paused = true
      console.log(player);
      // this.$refs.videoPlayer.player.pause();
    },
    //监听播放状态改变
    playerStateChanged (player) {
      // console.log(player);
    },
    //监听媒体是否已到达结尾，播放完
    onPlayerEnded (player) {
      this.paused = false
      console.log('player ended!', player)
      // console.log(player);
    },
    //DOM元素上的readyState更改导致播放停止。
    onPlayerWaiting (player) {
      // console.log(player);
    },
    //媒体不再被禁止播放，并且已开始播放。
    onPlayerPlaying (player) {
      // console.log(player);
    },
    //当播放器在当前播放位置下载数据时触发
    onPlayerLoadeddata (player) {
      // console.log(player);
    },
    //当前播放位置发生变化时触发。
    onPlayerTimeupdate (player) {
      // console.log(player);
      /*可以在onPlayerTimeupdate 中的player.cache_.currentTime获取到播放时间，所以可以保存下来：*/

      this.durations = player.cache_.currentTime

      // console.log(' onPlayerTimeupdate!', player)
    },
    //媒体的readyState为HAVE_FUTURE_DATA或更高
    onPlayerCanplay(player) {
      // console.log('player Canplay!', player)
    },
    //媒体的readyState为HAVE_ENOUGH_DATA或更高。这意味着可以在不缓冲的情况下播放整个媒体文件。
    onPlayerCanplaythrough(player) {
      // console.log('player Canplaythrough!', player)
    },
    //将侦听器绑定到组件的就绪状态。与事件监听器的不同之处在于，如果ready事件已经发生，它将立即触发该函数。。
    playerReadied(player) {
    /*  此处  写  后台  请求代码    */
      player.currentTime(34.868434);
      // seek to 10s
      console.log('example player 1 readied', player);
      // player.currentTime(0)
      // console.log('example 01: the player is readied', player)
    }


  },
  components: {
    videoPlayer
  }
}
</script>

<style scoped>

</style>
